﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using ScFramework.Core;
using UnityEngine.UI;
using System;

public class TabUILogic : LogicModuleBase<UIManager, SpriteManager>
{
    List<FuncDataPair> ColliderInfo;
    GameObject target;
    GameObject cell;
    Dictionary<string, Tuple<GameObject, FuncDataPair>> cellDIC;
    /// <summary>
    ///当前显示层级
    /// </summary>
    int nowLayer = 0;
    [RegisterEvent( FunctionalUIEvent.ShowKeycode)]
    void Init(List<FuncDataPair> Datas)
    {
        cellDIC = new Dictionary<string, Tuple<GameObject, FuncDataPair>>();
        target = SysModule.Open("TabUI", 3).gameObject;
        cell = target.transform.Find("Bench/Cell").gameObject;
        ColliderInfo = Datas;
        InitButtons();
        InitCell();
        Refresh(0);
    }
    [RegisterEvent(FunctionalUIEvent.LayerChanged)]
    void LayerChanged(int layer)
    {
        if (layer > 2)
        {
            nowLayer = 2;
        }
        else if (layer < -2)
        {
            nowLayer = -2;
        }
        else
        {
            nowLayer = layer;
        }
        Refresh(layer);
    }
    void InitCell()
    {
        int x = 0, y = 6;
        cell.name = x + "_" + y;
        for (int i = 0; i < 48; i++)
        {
            var t = GameObject.Instantiate(cell,target.transform.Find("Bench"));
            if (x < 6)
            {
                x++;
            }
            else
            {
                y--;
                x = 0;
            }

            t.name = x + "_" + y;
            cellDIC.Add(t.name, new Tuple<GameObject, FuncDataPair>(t, null));
        }
    }
    Button[] buttonList;
    void InitButtons()
    {
        buttonList = target.transform.GetComponentsInChildren<Button>(true);
        foreach (Button button in buttonList)
        {
            switch (button.name)
            {
                case "LayerUp":
                    button.onClick.AddListener(OnLayerUpClick);
                    break;
                case "LayerDown":
                    button.onClick.AddListener(OnLayerDownClick);
                    break;
            }
        }
        /// <summary>
        /// cell绑定对应层级的物体信息
        /// </summary>
        /// <param name="layer"></param>
        void Bunding(int layer)
        {
            foreach (var xx in ColliderInfo)
            {
                if (xx.EntityPos.z == layer)
                {
                    int x = xx.EntityPos.x;
                    int y = xx.EntityPos.y;
                    Tuple<GameObject, FuncDataPair> t;
                    cellDIC.TryGetValue(x + "_" + y, out t);
                    t = new Tuple<GameObject, FuncDataPair>(t.Item1, xx);
                }
            }
        }
        void Refresh(int layer)
        {
            Bunding(layer);
            foreach (var x in cellDIC.Values)
            {
                if (x.Item2 != null)
                {
                    x.Item1.transform.Find("Image").GetComponent<Image>().sprite = SysModule1.GetSprite(x.Item2.Body.SpriteName);
                }
            }

        }
    }
    void OnLayerUpClick()
    {
        Dispatch(FunctionalUIEvent.LayerChanged, nowLayer++);
    }
    void OnLayerDownClick()
    {
        Dispatch(FunctionalUIEvent.LayerChanged, nowLayer--);
    }
    /// <summary>
    /// cell绑定对应层级的物体信息
    /// </summary>
    /// <param name="layer"></param>
    void Bunding(int layer)
    {
        foreach (var xx in ColliderInfo)
        {
            if (xx.EntityPos.z == layer)
            {
                int x = xx.EntityPos.x;
                int y = xx.EntityPos.y;
                Tuple<GameObject, FuncDataPair> t;
                cellDIC.TryGetValue(x + "_" + y, out t);
                t = new Tuple<GameObject, FuncDataPair>(t.Item1, xx);
            }
        }
    }
    void Refresh(int layer)
    {
        Bunding(layer);
        foreach (var x in cellDIC.Values)
        {
            if (x.Item2 != null)
            {
                x.Item1.transform.Find("Image").GetComponent<Image>().sprite = SysModule1.GetSprite(x.Item2.Body.SpriteName);
                x.Item1.transform.Find("Mask/Text").GetComponent<Text>().text =x.Item2.Func.Key.ToString();
            }
        }

    }
}